package com.yao.dao.impl;

import com.yao.dao.RoleDao;
import com.yao.domain.Role;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class RoleDaoImpl implements RoleDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Role> findAll() {
        List<Role> roleList =
                jdbcTemplate.query("select * from sys_role", new BeanPropertyRowMapper<Role>(Role.class));
        return roleList;
    }

    @Override
    public void save(Role role) {
        jdbcTemplate.update("INSERT into sys_role values(?,?,?)",null,role.getRoleName(),role.getRoleDesc());
    }

    @Override
    public List<Role> findRolesByUserId(Long id) {
        List<Role> roles = jdbcTemplate.query("SELECT roleId,roleName,roleDesc" +
                        " FROM sys_user_role JOIN sys_role ON sys_user_role.roleId=sys_role.id WHERE userId=?"
                , new BeanPropertyRowMapper<Role>(Role.class),id);
        return roles;
    }
}
